import { ChatTarget } from "@/types/chatTarget"
import { defineStore } from "pinia"
import { ref } from "vue"

/*
 +----------------------------------------------------------------------
 + Title        : 公共 pinia 存储
 + Author       : huwl
 + Version      : 1.0.0
 + Initial-Time : 2024-12-24
 + Last-time    : 这个文件最后修改的时间 + 修改人的名称
 + Desc         : 公共 pinia 存储
 +----------------------------------------------------------------------
*/
export const useCommonStore = defineStore('common', () => {

    // 定义用户信息
    const currentRouteUrl = ref<string>('')

    // 聊天对象列表
    const chatTargetList = ref<ChatTarget[]>([])

    // 未读通知数量
    const unreadNotifyTotal = ref<number>(0)

    // 好友申请数量
    const friendApplyTotal = ref<number>(0)

    /**
     * 设置聊天对象列表
     * 
     * @param ChatTarget[] item ------------------------------------聊天对象
     * @returns void
     * @author huwl
     */
    const setChatTargetList = (items: ChatTarget[]) => {
        chatTargetList.value = items
    }

    /**
     * 添加
     * 
     * @param ChatTarget item --------------------------------------聊天对象
     * @returns void
     * @author huwl
     */
    const addChatTarget = (item: ChatTarget) => {
        chatTargetList.value.unshift(item)
    }

    /**
     * 修改
     * 
     * @param ChatTarget item --------------------------------------聊天对象
     * @returns void
     * @author huwl
     */
    const editChatTarget = (item: ChatTarget) => {
        const updateChatTarget:any = chatTargetList.value.find((value: ChatTarget) => value.id == item.id)
        for (const key in item) {
            updateChatTarget[key as never] = item[key as never]
        }
    }

    /**
     * 设置未读消息数量
     * 
     * @param number total -----------------------------------------数量
     * @returns void
     * @author huwl
     */
    const setUnreadNotifyTotal = (total: number) => {
        unreadNotifyTotal.value = total
    }

    /**
     * 设置好友申请数量
     * 
     * @param number total -----------------------------------------数量
     * @returns void
     * @author huwl
     */
    const setFriendApplyTotal = (total: number) => {
        friendApplyTotal.value = total
    }


    return {
        currentRouteUrl,
        chatTargetList,
        friendApplyTotal,
        unreadNotifyTotal,


        setChatTargetList,
        addChatTarget,
        editChatTarget,
        setFriendApplyTotal,
        setUnreadNotifyTotal
    }
},

// 持久化
{
    persist: true,
})